package com.jcj.jcjmq.demo;

import com.alibaba.fastjson.JSON;
import com.jcj.jcjmq.client.JcjBroker;
import com.jcj.jcjmq.client.JcjConsumer;
import com.jcj.jcjmq.model.Message;
import com.jcj.jcjmq.client.JcjProducer;

public class JcjMqDemo {
    public static void main(String[] args) {
        String topic = "jcj-queue";
        JcjBroker broker = JcjBroker.getDefaultBroker();
//        broker.createTopic(topic);

        JcjProducer producer = broker.createProducer();
        JcjConsumer consumer = broker.createConsumer(topic);
        consumer.listener(topic,message -> {
            System.out.println("onmessage：" + message);
        });

//        JcjConsumer consumer = broker.createConsumer(topic);

        for (int i = 0; i < 10; i++) {
            Order order = new Order((long) i, "item-" + i);
//            System.out.println("发送消息：" + order);
            producer.send(topic, new Message<>((long) i, JSON.toJSONString(order), null));
        }

//        for (int i = 0; i < 10; i++) {
//            JcjMessage message = consumer.recv(topic);
//            if (message != null) {
//                System.out.println("消费消息：" + message.getBody());
//            }
//            consumer.ack(topic, message);
//        }

    }
}
